//第三层

const mongoose = require('mongoose');

//获取出租方列表
module.exports.getUserAll3 = async (data) => {
    const data2 = data.type
    return await mongoose.model('usersModel').find({ type: data2 });
}

//获取出租方列表
module.exports.serchUserAll3 = async ({ value, input, rent }) => {
    if (rent == 'rent') {
        if (value == 'name') {
            return await mongoose.model('usersModel').find({ name: input,type:rent });
        } else if (value == 'phone') {
            return await mongoose.model('usersModel').find({ phone: input,type:rent });
        }
    }else if (rent == 'hire'){
        if (value == 'name') {
            return await mongoose.model('usersModel').find({ name: input,type:rent });
        } else if (value == 'phone') {
            return await mongoose.model('usersModel').find({ phone: input,type:rent });
        }
    }

}


//获取所有hire用户
module.exports.getAllHireUsers = async (param) => {
    const type = param.type;//查询的类型 hire
    let pageSize = param.pageSize;//每页显示条数
    let curPage = param.curPage;//当前页  
    
    //所有数据
    let all = await mongoose.model('usersModel').find({type});
    //总条数
    let total = all.length;
    //获取最大的页数
    let pages = Math.ceil(total / pageSize);
    //判断当前页是否超出最大范围
    if( curPage > pages ){
        curPage = pages;
    }    
    let rows = await mongoose.model('usersModel').find({type})
    .limit(pageSize - 0)   //返回前端几条数据，里面是一个number
    .skip( (curPage - 1) * pageSize ); //跳页
    return {total, rows, pages, pageSize, curPage};
    //return await mongoose.model('usersModel').find({type});
}
//名字查询 
module.exports.queryHireByName = async (p) => {
    return await mongoose.model('usersModel').find(p);
}
//通过电话号码查询 
module.exports.queryHireByPhone = async (p) => {
    return await mongoose.model('usersModel').find(p);
}
//查询所有待审核的用户  
module.exports.getUnUsers = async (param) => {
    const authentication = param.authentication;//查询的类型 hire
    let pageSize = param.pageSize;//每页显示条数
    let curPage = param.curPage;//当前页  
    
    //所有数据
    let all = await mongoose.model('usersModel')
    .find({authentication});
    //总条数
    let total = all.length;
    //获取最大的页数
    let pages = Math.ceil(total / pageSize);
    //判断当前页是否超出最大范围
    if( curPage > pages ){
        curPage = pages;
    }    
    let rows = await mongoose.model('usersModel').find({authentication})
    .limit(pageSize - 0)   //返回前端几条数据，里面是一个number
    .skip( (curPage - 1) * pageSize ); //跳页
    return {total, rows, pages, pageSize, curPage};
    //return await mongoose.model('usersModel').find({type});
}
//修改用户状态 
module.exports.updateUserState = async ({_id,authentication}) => {
    return await mongoose.model('usersModel').updateOne({_id},{authentication});
}